package org.dromara.bean.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.constant.OrderConstant;
import org.dromara.bean.constant.BusinessCodeTypeConstant;
import org.dromara.bean.constant.OrderStatusConstant;
import org.dromara.bean.domain.HisGoodsOrder;
import org.dromara.bean.mapper.HisGoodsMapper;
import org.dromara.bean.mapper.HisGoodsOrderMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.bean.domain.bo.HisGoodsOrderEvaluateBo;
import org.dromara.bean.domain.vo.HisGoodsOrderEvaluateVo;
import org.dromara.bean.domain.HisGoodsOrderEvaluate;
import org.dromara.bean.mapper.HisGoodsOrderEvaluateMapper;
import org.dromara.bean.service.IHisGoodsOrderEvaluateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商城订单评价Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-26
 */
@RequiredArgsConstructor
@Service
public class HisGoodsOrderEvaluateServiceImpl implements IHisGoodsOrderEvaluateService {

    private final HisGoodsOrderEvaluateMapper baseMapper;
    private final HisGoodsOrderMapper goodsOrderMapper;

    private final HisGoodsMapper goodsMapper;

    /**
     * 查询商城订单评价
     *
     * @param id 主键
     * @return 商城订单评价
     */
    @Override
    public HisGoodsOrderEvaluateVo queryById(Long id) {
        HisGoodsOrderEvaluateVo hisGoodsOrderEvaluateVo = baseMapper.selectVoById(id);
        hisGoodsOrderEvaluateVo.setGoods(goodsMapper.selectById(hisGoodsOrderEvaluateVo.getGoodsId()));

        return hisGoodsOrderEvaluateVo;
    }


    /**
     * 分页查询商城订单评价列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商城订单评价分页列表
     */
    @Override
    public TableDataInfo<HisGoodsOrderEvaluateVo> queryPageList(HisGoodsOrderEvaluateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisGoodsOrderEvaluate> lqw = buildQueryWrapper(bo);
        Page<HisGoodsOrderEvaluateVo> result = baseMapper.selectPageHisGoodsOrderEvaluateList(pageQuery.build(), lqw);

        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商城订单评价列表
     *
     * @param bo 查询条件
     * @return 商城订单评价列表
     */
    @Override
    public List<HisGoodsOrderEvaluateVo> queryList(HisGoodsOrderEvaluateBo bo) {
        LambdaQueryWrapper<HisGoodsOrderEvaluate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisGoodsOrderEvaluate> buildQueryWrapper(HisGoodsOrderEvaluateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisGoodsOrderEvaluate> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotEmpty(bo.getStatus()), HisGoodsOrderEvaluate::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotEmpty(bo.getOrderNumber()), HisGoodsOrderEvaluate::getOrderNumber, bo.getOrderNumber());
        lqw.eq(ObjectUtil.isNotEmpty(bo.getEvaluate()), HisGoodsOrderEvaluate::getEvaluate, bo.getEvaluate());
        lqw.orderByDesc(HisGoodsOrderEvaluate::getCreateTime);
        return lqw;
    }

    /**
     * 新增商城订单评价
     *
     * @param bo 商城订单评价
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(List<HisGoodsOrderEvaluateBo> bo) {
        Long userId = LoginHelper.getUserId();
        boolean flag = false;
        for (HisGoodsOrderEvaluateBo obj : bo) {
            if (ObjectUtil.isEmpty(obj.getOrderNumber())) {
                throw new RuntimeException("订单号不能为空");
            }

            HisGoodsOrder goodsOrder = goodsOrderMapper.selectOne(new LambdaQueryWrapper<HisGoodsOrder>().eq(HisGoodsOrder::getOrderNumber, obj.getOrderNumber()));
            if (!goodsOrder.getMemberId().equals(userId)) {
                throw new RuntimeException("订单不属于当前用户");
            }
            HisGoodsOrderEvaluate add = MapstructUtils.convert(obj, HisGoodsOrderEvaluate.class);
            add.setMemberId(userId);
//            add.setGoodsId(goodsOrder.getId());
            add.setStatus(100l);
            validEntityBeforeSave(add);
            flag = baseMapper.insert(add) > 0;

            if (flag) {
                goodsOrder.setStatus(OrderStatusConstant.SHOP_EVALUATED);
                goodsOrderMapper.updateById(goodsOrder);
            }
        }

        return flag;
    }

    /**
     * 修改商城订单评价
     *
     * @param bo 商城订单评价
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HisGoodsOrderEvaluateBo bo) {
        HisGoodsOrderEvaluate update = MapstructUtils.convert(bo, HisGoodsOrderEvaluate.class);
        if (bo.getStatus() != 103) {
            update.setReason("");
        }
        validEntityBeforeSave(update);

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisGoodsOrderEvaluate entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除商城订单评价信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
